jQuery(function(){
	jQuery('.DropShadow').each(function() {
		var offsetHeight = this.offsetHeight;
		var offsetWidth = this.offsetWidth;
		if(offsetHeight == 0) {
			var clone = this.cloneNode(true);
			clone.style.position = 'absolute';
			clone.style.left = '-10000px';
			clone.style.top = '-10000px';
			clone.style.display = 'block';
			document.body.appendChild(clone);
			offsetHeight = clone.offsetHeight;
			offsetWidth = clone.offsetWidth;
			document.body.removeChild(clone);
		}

		jQuery(this).wrap('<div class="DropShadowContainer"><div class="Shadow1"><div class="Shadow2"><div class="Shadow3"><div class="ItemContainer"></div></div></div></div></div>');
		var container = this.parentNode.parentNode.parentNode.parentNode.parentNode;

		jQuery(container).css('height', offsetHeight+"px");
		jQuery(container).css('position', this.style.position);
		jQuery(container).css('top', this.style.top);
		jQuery(container).css('left', this.style.left);
		jQuery(container).css('display', this.style.display);
		jQuery(container).attr('id', this.id);
		jQuery(this).css('position', 'static');
		jQuery(this).css('display', '');
		jQuery(this).removeClass('DropShadow');
		this.id = '';
	});

	jQuery('.PopDownMenu').each(function() {
		jQuery(this).click(function(e) {
			closeMenu();
			if(document.topCurrentMenu) {

				jQuery(document.topCurrentMenu).hide();
				jQuery(document.topCurrentButton).removeClass('ActiveButton');
			}
			var id = this.id.replace(/Button$/, '');
			if(!('#'+id))
				return false;
			var menu = jQuery('#'+id);

			var obj = this;
			offsetTop = 0;
			offsetLeft = 0;
			overPositioned = false;
			pageOffsetLeft = 0;
			while(obj)
			{
				if(!overPositioned) {
					offsetLeft += obj.offsetLeft;
					offsetTop += obj.offsetTop;
				}
				pageOffsetLeft += obj.offsetLeft;
				obj = obj.offsetParent;
				if(obj && CurrentStyle(obj, 'position')) {
					var pos = CurrentStyle(obj, 'position');
					if(pos == "absolute" || pos == "relative") {
						overPositioned = true;
					}
				}
			}

			jQuery(this).addClass('ActiveButton');

			// hide plugins like flash
			jQuery('embed, object').css({ visibility: 'hidden' });

			jQuery(menu).css('position', 'absolute');
			jQuery(menu).css('visibility', 'hidden');
			jQuery(menu).css('display', '');
			jQuery(menu).addClass('PopDownMenuContainer');

			// The Form Fields add field button
			if (jQuery(this).hasClass('FormFieldsMenuButton')) {
				jQuery(menu).css('top', offsetTop+this.offsetHeight+3+"px");
				this.blur();
				jQuery(menu).css('left', offsetLeft+3 + "px");
			} else {
				jQuery(menu).css('top', offsetTop+this.offsetHeight+1+"px");
				this.blur();
				var menuWidth = jQuery(menu).get(0).offsetWidth;
				if(pageOffsetLeft + menuWidth > jQuery(window).width()) {
					jQuery(menu).css('left', (offsetLeft - menuWidth + jQuery(this).get(0).offsetWidth + 2) + 'px');
				}
				else {
					jQuery(menu).css('left', offsetLeft+2+ "px");
				}
			}

			jQuery(menu).css('visibility', 'visible');
			jQuery(menu).show();

			// show any plugins inside the actual menu dom which were hidden above, like swfupload elements as menu items
			jQuery('embed, object', menu).css({ visibility: 'visible' });

			e.stopPropagation();
			jQuery(document).click(function() {
				jQuery(menu).hide(); jQuery(document.topCurrentButton).removeClass('ActiveButton');
				document.topCurrentMenu = '';
				jQuery('.ControlPanelSearchBar').show();
				jQuery('embed, object').css({ visibility: 'visible' });
			});
			document.topCurrentMenu = menu;
			document.topCurrentButton = this;
			return false;
		});
	});

});

function CurrentStyle(element, prop) {
	if(element.currentStyle) {
		return element.currentStyle[prop];
	}
	else if(document.defaultView && document.defaultView.getComputedStyle) {
		prop = prop.replace(/([A-Z])/g,"-$1");
		prop = prop.toLowerCase();
		return document.defaultView.getComputedStyle(element, "").getPropertyValue(prop);
	}
}

function openPopup(url, title)
{
	var l = screen.availWidth / 2 - 450;
	var t = screen.availHeight / 2 - 320;
	var win = window.open(url, title, 'width=800,height=650,left='+l+',top='+t+',scrollbars=1');
	return false;
}

jQuery(function(){
	// For IE, set the last child
	jQuery('.MenuText a.MenuText:last-child').addClass('Last');
});
